package QuizUI;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import QuizPackage.Question;
import QuizPackage.Quiz;

/**
 * Servlet implementation class QuestionServlet
 */
@WebServlet("/QuestionServlet")
public class QuestionServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public QuestionServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		Quiz quiz = (Quiz) session.getAttribute(request.getParameter("quizid"));
		int quizid = Integer.parseInt(request.getParameter("quizid"));
		
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		
		if(quiz.immediateCorrection())
		{	
			RequestDispatcher dispatch = request.getRequestDispatcher("Correction.jsp");
			dispatch.forward(request,response);
			return;
		}else{
			Enumeration<String> params = request.getParameterNames();
			while(params.hasMoreElements())
			{
				String questionIDString = params.nextElement();
				if(!questionIDString.equals("quizid") && !questionIDString.equals("submit"))
				{
					int questionID = Integer.parseInt(questionIDString);
					Question q = quiz.getQuestion(questionID);
					int score = q.checkAnswer(request.getParameterValues(questionIDString));
					quiz.recordScore(score,q.totalPoints(),questionID);
				}
			}
			doPost(request,response);
			
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		RequestDispatcher dispatch = request.getRequestDispatcher("DisplayQuestion.jsp");
		dispatch.forward(request,response);
		return;
		/*
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		
		HttpSession session = request.getSession();
		Quiz quiz = (Quiz) session.getAttribute(request.getParameter("quizid"));
		int quizid = Integer.parseInt(request.getParameter("quizid"));
		
		if(request.getParameter("submit")!=null && request.getParameter("submit").equals("Practice Mode")) quiz.setPracticeMode(true);
		if(request.getParameter("submit")!=null && request.getParameter("submit").equals("Review")) quiz.setReviewMode(true);
		
		if(!quiz.isStarted()) quiz.startQuiz();
		
		Question nextQ = quiz.nextQuestion();
		if(nextQ==null)
		{
			RequestDispatcher dispatch2 = request.getRequestDispatcher("EndQuiz");
			dispatch2.forward(request,response);
			return;
		}
		
		out.println("<?xml version=\"1.0\" encoding=\"ISO-8859-1\" ?>");
		out.println("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\""
				      + " \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">");
		out.println("<html xmlns='http://www.w3.org/1999/xhtml'>");
		out.println("<head>");
		
		out.println("<title>"+quiz.getName()+"</title>");
		out.println("</head>");
		
		out.println("<body>");

		out.println("<form action=\"QuestionServlet\" method=\"get\">");
		out.println("<input name=\"quizid\" type =\"hidden\" value=\""+quizid+"\"/>");
		
		out.println(nextQ.getHTML());
		if(quiz.singlePage())
		{
			nextQ=quiz.nextQuestion();
			while(nextQ!=null)
			{
				out.println(nextQ.getHTML());
				nextQ=quiz.nextQuestion();
			}
		}
		
		if(quiz.isReview()) out.println("<input type=\"submit\" value=\"Review\">");
		else out.println("<input type=\"submit\" value=\"Submit\">");
		out.println("</form>");
		out.println("</body>");
		out.println("</html>");*/
	}
}
